from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from meiduo_admin.serializers.images import SKUImageSerialzier, SKUSerialzier
from meiduo_admin.utils import PageNum
from goods.models import SKUImage,SKU


class ImageView(ModelViewSet):
    # 指定查询集
    queryset = SKUImage.objects.all()
    # 指定序列化器
    serializer_class = SKUImageSerialzier
    # 指定权限
    permission_classes = [IsAdminUser]
    # 指定分页器
    pagination_class = PageNum

    # 获取sku表的所有id值
    def simple(self, request):
        # 1、查询所有spu表数据
        skus = SKU.objects.all()
        # 2、序列化返回
        ser = SKUSerialzier(skus, many=True)

        return Response(ser.data)


    # def create(self, request, *args, **kwargs):
    #     # 完整序列化和反序列化的整个业务逻辑
    #     #1、获取前端数据
    #     data=request.data
    #     #2、验证数据  调用序列化器  is_valid()
    #     ser=self.get_serializer(data=data)
    #     ser.is_valid()
    #     #3、保存数据  调用序列化器  save() 会调用序列化器中封装的create()方法
    #     ser.save()
    #     #4、结果返回  调用序列化器
    #     return Response({'data':'视图'},status=201)